//
//  EYTouchController.m
//  touch
//
//  Created by easy on 11-5-25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EYTouchController.h"

#pragma mark -
#pragma mark EYTouchController
@implementation EYTouchController
@synthesize touches;
@synthesize touchCount;
@synthesize delegate;
 
-(void) touchesBegan:(NSSet *)_touches event:(UIEvent *)event{
	NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:[_touches count]];
	for (UITouch *touch in _touches) {
		if (![self exists:touch]) {	//加入未管理的uitouch
			EYTouch *t = [[EYTouch alloc] initWithTouch:touch];
			[t restart];//init 里其实会调用
			[arr addObject:t];
			[self.touches addObject:t];
		}
	}
	if ([self.delegate respondsToSelector:@selector(didTouchBegan:event:controller:)]) {
		[self.delegate didTouchBegan:arr event:event controller:self];
	}
	[arr release];
}
 
-(void) touchesEnded:(NSSet *)_touches event:(UIEvent *)event{
	NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:[_touches count]];
	for (UITouch *touch in _touches) {
		EYTouch *t = [self touchFor:touch];
		if (t != nil) {//移除管理组中对应的touch
			[arr addObject:t];//先add再remove，remove可能会release掉
			[self.touches removeObject:t]; 
		}
	}
	if ([self.delegate respondsToSelector:@selector(didTouchEnded:event:controller:)]) {
		[self.delegate didTouchEnded:arr event:event controller:self];
	}
	[arr release];
}
 
-(void) touchesMoved:(NSSet *)_touches event:(UIEvent *)event{
	NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:[_touches count]];
	for (UITouch *touch in _touches) {
		EYTouch *t = [self touchFor:touch];
		if (t != nil) {
			[arr addObject:t];
		}
	}
	if ([self.delegate respondsToSelector:@selector(didTouchMoved:event:controller:)]) {
		[self.delegate didTouchMoved:arr event:event controller:self];
	}
	[arr release];
}
 
-(void) touchesCancelled:(NSSet *)_touches event:(UIEvent *)event{
	NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:[_touches count]];
	for (UITouch *touch in _touches) {
		EYTouch *t = [self touchFor:touch];
		if (t != nil) {//移除管理组中对应的touch
			[arr addObject:t];
			[self.touches removeObject:t];
		}
	}
	if ([self.delegate respondsToSelector:@selector(didTouchCancelled:event:controller:)]) {
		[self.delegate didTouchCancelled:arr event:event controller:self];
	}
	[arr release];
}
-(EYTouch *) touchAtIndex:(NSUInteger) index{
	return [self.touches objectAtIndex:index];
}
-(NSMutableArray *) touches{
	if (!touches) {
		touches = [[NSMutableArray alloc] initWithCapacity:3];//一般不会超过3个点
	}
	return touches;
}
-(int) touchCount{
	return [self.touches count];
}

-(BOOL) exists : (UITouch *)touch{
	return [self touchFor:touch] != nil;
}
-(EYTouch *) touchFor : (UITouch *)touch{
	for (EYTouch *t in self.touches) {
		if (t.touch == touch) {
			return t;
		}
	}
	return nil;
}
-(void) dealloc{
	[touches release];
	[super dealloc];
}
@end


#pragma mark -
#pragma mark EYTouch
@implementation EYTouch
@synthesize touch;
@synthesize current;
@synthesize start;
-(id) initWithTouch:(UITouch *) _touch{
	if (self = [super init]) {
		touch = [_touch retain];		//retain。最后release
		[self restart];					//重设start点
	}
	return self;
}

-(CGPoint) current{
	return [self.touch locationInView:self.touch.view];		//当前的是相对touch所在view的点
}
-(void) restart{
	start = self.current;
}
-(void) dealloc{
	[touch release];
	[super dealloc];
}
-(CGPoint) offset{
	return CGPointMake(self.current.x - self.start.x, self.current.y - self.start.y);
}
-(float) scaleTo:(EYTouch *) _touch{
	float vector1 = sqrt((_touch.start.x - self.start.x) * (_touch.start.x - self.start.x) + 
						 (_touch.start.y - self.start.y) * (_touch.start.y - self.start.y));
	float vector2 = sqrt((_touch.current.x - self.current.x) * (_touch.current.x - self.current.x) + 
						 (_touch.current.y - self.current.y) * (_touch.current.y - self.current.y));
	return vector2 / vector1;
}
-(float) rotateTo:(EYTouch *) _touch{
	float vectorRadian1 = atan((_touch.start.y - self.start.y)/(_touch.start.x - self.start.x));
	float vectorRadian2 = atan((_touch.current.y - self.current.y)/(_touch.current.x - self.current.x));
	
	return vectorRadian2 - vectorRadian1;
}
-(CGPoint) translateTo:(EYTouch *) _touch{
	return CGPointMake((_touch.offset.x + self.offset.x)/2,(_touch.offset.y + self.offset.y)/2);
}
@end
